<template>
  <div class="app-container">
    <el-form :inline="true" :model="listQuery" @submit.native.prevent>
      <el-form-item label="规格名称">
        <el-input size="small" v-model="listQuery.q_name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="this.getList">查询</el-button>
        <el-button icon="el-icon-refresh-left" size="small" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="button-table-margin">
      <el-button size="small" type="primary" @click="addBrand" v-permission="['admin/product/norm/create']">添加</el-button>
      <el-button size="small" type="primary" @click="editBrand" v-permission="['admin/product/norm/update']">修改</el-button>
      <el-button size="small" type="primary" @click="deleteBrand" v-permission="['admin/product/norm/del']"
        >删除</el-button
      >
    </div>
    <el-table
      ref="singleTable"
      :data="dataList"
      v-loading="listLoading"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @current-change="handleCurrentChange"
    >
      <el-table-column type="index" align="center" width="50">
      </el-table-column>
      <el-table-column show-overflow-tooltip property="name" label="规格名称"> </el-table-column>
      <el-table-column property="sort" label="权重"> </el-table-column>
      <el-table-column property="createUser" label="创建人"> </el-table-column>
      <el-table-column width="160" property="createTime" label="创建时间">
      </el-table-column>
      <el-table-column property="updateUser" label="修改人"> </el-table-column>
      <el-table-column width="160" property="updateTime" label="修改时间">
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { normList, deleteNorm } from "@/api/product";
import { getCodeManager } from "@/api/all";
import Pagination from "@/components/Pagination";
import { Message } from "element-ui";
export default {
  name: "Norm",
  components: { Pagination },
  data() {
    return {
      dataList: [],
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        q_name: ""
      },
      currentRow: null
    };
  },
  created() {
    getCodeManager("BRAND_TOP", () => {});
    this.getList();
  },
  methods: {
    handleCurrentChange(val) {
      // 选中当前行数据
      this.currentRow = val;
    },
    // 获取列表数据
    getList() {
      this.listLoading = true;
      normList(this.listQuery).then(response => {
        this.dataList = response.data.rows;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    reset() {
      this.listQuery.q_name = "";
      this.getList();
    },
    addBrand() {
      this.$router.push({ path: "/product/normadd" });
    },
    editBrand() {
      if (!this.currentRow) {
        Message.error("请选择操作行。");
        return;
      }
      this.$router.push({
        path: "/product/normedit",
        query: { id: this.currentRow.id }
      });
    },
    deleteBrand() {
      if (!this.currentRow) {
        Message.error("请选择操作行。");
        return;
      }
      this.$confirm("确定删除吗？", "提示", {
        onfirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteNorm({ id: this.currentRow.id }).then(res => {
          if (res.data.success) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.getList();
          } else {
            this.$message({
              type: "error",
              message: res.data.message
            });
          }
        });
      });
    }
  }
};
</script>
<style scoped>
.filter-container .el-col-6 {
  margin-bottom: 15px;
}
.filter-item {
  margin-bottom: 0;
}
/deep/ .filter-container label {
  font-size: 14px;
  font-weight: 500;
  margin-right: 10px;
}
</style>
